package org.skydiveapplibrary.util;

import java.util.List;

import org.skydiveapplibrary.R;
import org.skydiveapplibrary.data.Rig;
import org.skydiveapplibrary.data.RigReminder.DueStatus;
import org.skydiveapplibrary.repository.RigRepository;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.view.ContextThemeWrapper;

public class RigReminderManager
{
	// singleton instance
	private static RigReminderManager instance = new RigReminderManager();
	
	// check required
	private boolean reminderCheckRequired = false;
	
	private RigReminderManager()
	{
		// initially true
		reminderCheckRequired = true;
	}
	
	public static RigReminderManager getInstance()
	{
		return instance;
	}
	
	public void remindersChanged()
	{
		reminderCheckRequired = true;
	}
	
	public void showRigReminderAlert(Context context)
	{
		// if check not required, exit
		if (!reminderCheckRequired)
			return;
		
		// reset flag
		reminderCheckRequired = false;

		if (hasDueReminders(context))
		{
			Builder builder = new Builder(new ContextThemeWrapper(context, R.style.DialogStyle));
			builder.setIcon(android.R.drawable.ic_dialog_alert);
			builder.setPositiveButton(android.R.string.ok, null);
			builder.setTitle(R.string.main_gear_reminder_dialog_title);
			builder.setMessage(R.string.main_gear_reminder_dialog_message);
			builder.create().show();
		}
	}
	
	private boolean hasDueReminders(Context context)
	{
		// get rigs
		RigRepository rigRepo = new RigRepository(context);
		List<Rig> rigs = rigRepo.getRigs();
		
		// get due status
		DueStatus dueStatus = null;
		for (Rig rig: rigs)
		{
			dueStatus = rig.getMostUrgentDueStatus();
			if (dueStatus == DueStatus.PastDue ||
					dueStatus == DueStatus.DueSoon)
			{
				return true;
			}
		}
		return false;
	}
}
